//
//  TimeCountView.m
//  test内付
//
//  Created by csh on 16/7/19.
//  Copyright © 2016年 陈世豪. All rights reserved.
//

#import "TimeCountView.h"
@interface TimeCountView()
@property(nonatomic,weak)UILabel *hourLabel;
@property(nonatomic,weak)UILabel *minuteLabel;
@property(nonatomic,weak)UILabel *secondLabel;
@property(nonatomic,weak)UILabel *firstMaoHaoLabel;
@property(nonatomic,weak)UILabel *secondMaoHaoLabel;
@property(nonatomic,assign)NSTimeInterval time;
@property(nonnull,strong)NSTimer *countTimer;
@end
@implementation TimeCountView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame: frame]) {
        [self initUI];
    }
    return self;
}

-(void)timerStartWithTime:(NSTimeInterval)time {
    self.time=time;
    self.countTimer=[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(countTime) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.countTimer forMode:NSRunLoopCommonModes];
    
}
-(void)countTime{
//    NSDate *date=[NSDate date];
//    NSTimeInterval today=[date timeIntervalSince1970];
//    NSUInteger delta=self.time-today;
    NSDate *endDate=[NSDate dateWithTimeIntervalSince1970:self.time];
    NSUInteger delta=[endDate timeIntervalSinceNow];
//    NSInteger hour = (int)(delta/3600);
//    NSInteger minute = (int)(delta - hour*3600)/60;
//    NSInteger second = delta - hour*3600 - minute*60;
        NSInteger hour = (int)delta/3600;
        NSInteger minute = (delta%3600)/60;
        NSInteger second = delta%60;
    NSString *hourStr=[NSString stringWithFormat:@"%02ld",(long)hour];
    NSString *minuteStr=[NSString stringWithFormat:@"%02ld",(long)minute];
    NSString *secondStr=[NSString stringWithFormat:@"%02ld",(long)second];
    if (delta==0) {
       self.hourLabel.text=@"00";
        self.minuteLabel.text=@"00";
        self.secondLabel.text=@"00";
        [self.countTimer invalidate];
        if ([self.delegate respondsToSelector:@selector(timerDidStop)]) {
            [self.delegate timerDidStop];
        }
        return;
    }
    if (![self.hourLabel.text isEqualToString:hourStr]) {
        self.hourLabel.text=hourStr;

    }
    if (![self.minuteLabel.text isEqualToString:minuteStr]) {
        self.minuteLabel.text=minuteStr;

    }
    if (![self.secondLabel.text isEqualToString:secondStr]) {
        self.secondLabel.text=secondStr;
    }

}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.hourLabel.frame=CGRectMake(0, 0, 30,30);
    CGFloat fmX=CGRectGetMaxX(self.hourLabel.frame)-0.5;
    self.firstMaoHaoLabel.frame=CGRectMake(fmX, 0, 30, 30);
    CGFloat minX=CGRectGetMaxX(self.firstMaoHaoLabel.frame);
    self.minuteLabel.frame=CGRectMake(minX, 0, 30, 30);
    CGFloat smX=CGRectGetMaxX(self.minuteLabel.frame)-0.5;
    self.secondMaoHaoLabel.frame=CGRectMake(smX, 0, 30, 30);
    CGFloat secX=CGRectGetMaxX(self.secondMaoHaoLabel.frame);
    self.secondLabel.frame=CGRectMake(secX, 0, 30, 30);
}
-(void)initUI{
    UILabel *hourLabel=[[UILabel alloc]init];
    hourLabel.textAlignment=NSTextAlignmentCenter;
    hourLabel.layer.borderColor=[UIColor redColor].CGColor;
    hourLabel.layer.borderWidth=0.5;
    hourLabel.font=[UIFont systemFontOfSize:13];
    [self addSubview:hourLabel];
    self.hourLabel=hourLabel;
    UILabel *minuteLabel=[[UILabel alloc]init];
    minuteLabel.textAlignment=NSTextAlignmentCenter;
    minuteLabel.layer.borderColor=[UIColor redColor].CGColor;
    minuteLabel.layer.borderWidth=0.5;
    minuteLabel.font=[UIFont systemFontOfSize:13];

    [self addSubview:minuteLabel];
    self.minuteLabel=minuteLabel;
    UILabel *secondLabel=[[UILabel alloc]init];
    secondLabel.textAlignment=NSTextAlignmentCenter;
    secondLabel.layer.borderColor=[UIColor redColor].CGColor;
    secondLabel.layer.borderWidth=0.5;
    secondLabel.font=[UIFont systemFontOfSize:13];

    [self addSubview:secondLabel];
    self.secondLabel=secondLabel;
    UILabel *firstMaoHaoLabel=[[UILabel alloc]init];
    firstMaoHaoLabel.textAlignment=NSTextAlignmentCenter;
    firstMaoHaoLabel.text=@":";
    firstMaoHaoLabel.font=[UIFont systemFontOfSize:13];

    [self addSubview:firstMaoHaoLabel];
    self.firstMaoHaoLabel=firstMaoHaoLabel;
    UILabel *secondMH=[[UILabel alloc]init];
    secondMH.textAlignment=NSTextAlignmentCenter;
    secondMH.text=@":";
    secondMH.font=[UIFont systemFontOfSize:13];

    [self addSubview:secondMH];
    self.secondMaoHaoLabel=secondMH;
}
@end
